package j20251013;

import java.io.File;
import java.util.Scanner;

public class Demo1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要搜索路径：");
        String path=sc.next();
        File file=new File(path);
        if(!file.isDirectory()){
            System.out.println("输入的不是目录！");
            return;
        }
        System.out.println("请输入要搜索的关键字！");
        String name=sc.next();
        scanDir(file,name);
    }

    private static void scanDir(File file, String name) {
        //1.列出目录中包含的内容
        File[] files = file.listFiles();
        if(files==null){
            return;
        }
        //2.遍历目录
        for(File f:files){
            System.out.println(f.getAbsoluteFile());
            if(f.isFile()){
                //3.处理普通文件
                dealFile(f,name);
            }else{
                scanDir(f,name);
            }
        }
    }

    private static void dealFile(File f, String name) {
        if(f.getName().contains(name)){
            System.out.println("发现目标文件"+f.getAbsoluteFile()+"是否删除文件（t,f）?");
            Scanner sc = new Scanner(System.in);
            String choice=sc.next();
            if(choice.equals("t")){
                f.delete();
                System.out.println("文件已删除！");
            }
        }
    }
}
